package com.langrensha.langrensha.store;

import com.langrensha.langrensha.entity.Room;
import com.langrensha.langrensha.entity.RoomStatus;
import com.langrensha.langrensha.entity.UserInfo;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.*;

public class RoomStore {

    public static final Map<String, ChannelGroup> HOST_CHANNELS = new HashMap<>();
    public static final Map<String, ChannelGroup> HOST_SEAT_CHANNELS = new HashMap<>();
    public static final Map<String, List<String>> HOST_KILL_INFO = new HashMap<>();
    public static final Map<String, List<String>> HOST_NIGHT_INFO = new HashMap<>();
    public static final Map<String, Room> ROOM_MAP = new HashMap<>();
    public static final Set<Integer> ROOM_ID = new HashSet<>();
    public static final Map<String, List<UserInfo>> HOST_SEAT_USER_MAP = new HashMap<>();

    public static Map<Integer, String> role = new HashMap<>();

    public static List<String> IMAGES = new ArrayList<>();

    static {
        for (int i = 0; i < 24; i++) {
            IMAGES.add(String.valueOf(i + 1));
        }
        role.put(1, "预言家");
        role.put(2, "女巫");
        role.put(3, "猎人");
        role.put(4, "白神");
        role.put(5, "守卫");
        role.put(21, "白狼王");
        role.put(22, "狼王");
        role.put(10, "村民"); // 大于10 小于20为村民
        role.put(30, "狼人"); // 大于30 是狼人
    }

    public static ChannelGroup getRoomChannel(String hostId) {
        return HOST_CHANNELS.get(hostId);
    }

    public static ChannelGroup getSeatChannel(String hostId) {
        return HOST_SEAT_CHANNELS.get(hostId);
    }
    public static List<UserInfo> getSeatUser(String hostId) {
        return HOST_SEAT_USER_MAP.get(hostId);
    }

    public static List<Integer> getRole(String hostId) {
        return ROOM_MAP.get(hostId).getRole();
    }

    public static Room getRoom(String hostId) {
        return ROOM_MAP.get(hostId);
    }

    public static Integer save(Room room) {
        Integer hostId = new Random().nextInt(100000) + 100000 + 100000;
        while (!ROOM_ID.add(hostId)) {
            hostId = new Random().nextInt(100000) + 100000 + 100000;
        }
        String hostIdStr = String.valueOf(hostId);
        ROOM_MAP.put(hostIdStr, room);
        HOST_CHANNELS.put(hostIdStr, new DefaultChannelGroup(GlobalEventExecutor.INSTANCE));
        HOST_SEAT_CHANNELS.put(hostIdStr, new DefaultChannelGroup(GlobalEventExecutor.INSTANCE));
        HOST_KILL_INFO.put(hostIdStr, new ArrayList<>());
        HOST_NIGHT_INFO.put(hostIdStr, new ArrayList<>());
        HOST_SEAT_USER_MAP.put(hostIdStr, new ArrayList<>());

        List<String> images = new ArrayList<>(IMAGES);
        room.setImages(images);
        room.setImageUsed(new ArrayList<>());
        return hostId;
    }

    public static Map<String, String> getSeatInfo(String hostId) {
        return ROOM_MAP.get(hostId).getSeatInfo();
    }

    public static List<String> getKillInfo(String hostId) {
        return HOST_KILL_INFO.get(hostId);
    }

    public static void addKillInfo(String hostId, String info) {
        HOST_KILL_INFO.get(hostId).add(info);
    }

    public static List<String> getNightInfo(String hostId) {
        return HOST_NIGHT_INFO.get(hostId);
    }

    public static void addNightInfo(String hostId, String info) {
        HOST_NIGHT_INFO.get(hostId).add(info);
    }

    public static String getPoison(String hostId) {
        return ROOM_MAP.get(hostId).getPoison();
    }

    public static void setPoison(String hostId, String poison) {
        ROOM_MAP.get(hostId).setPoison(poison);
    }

    public static void addChannel(String hostId, Channel channel) {
        HOST_CHANNELS.get(hostId).add(channel);
    }

    public static void addSeatChannel(String hostId, Channel channel) {
        HOST_SEAT_CHANNELS.get(hostId).add(channel);
    }
    public static void addSeatUser(String hostId, UserInfo u) {
        HOST_SEAT_USER_MAP.get(hostId).add(u);
    }

    public static void removeSeatChannel(String hostId, Channel channel) {
        HOST_SEAT_CHANNELS.get(hostId).remove(channel);
    }
    public static void removeSeatUser(String hostId, UserInfo u) {
        HOST_SEAT_USER_MAP.get(hostId).remove(u);
    }

    public static void begin(String hostId) {
        ROOM_MAP.get(hostId).setStatus(RoomStatus.BEGIN);
    }

    public static void replaceChannel(String hostId, Channel channel, Channel channel1) {
        HOST_CHANNELS.get(hostId).remove(channel);
        HOST_CHANNELS.get(hostId).add(channel1);
        HOST_SEAT_CHANNELS.get(hostId).remove(channel);
        HOST_SEAT_CHANNELS.get(hostId).add(channel1);
    }

    public static void removeChannel(String hostId, Channel channel) {
        HOST_CHANNELS.get(hostId).remove(channel);
        HOST_SEAT_CHANNELS.get(hostId).remove(channel);
    }

    public static void addSeatInfo(String hostId, String seatId, String userName) {
        ROOM_MAP.get(hostId).getSeatInfo().put(seatId, userName);
    }

    public static void removeSeatInfo(String hostId, String seatId) {
        ROOM_MAP.get(hostId).getSeatInfo().remove(seatId);
    }
}
